<?php

/**
 * Created by PhpStorm.
 * User: li
 * Date: 15-10-18
 * Time: 下午11:27
 */
class Activity extends MY_Admin_Controller
{
    function __construct(){
        parent::__construct();
        //解析站点信息
        $this->load->helper('file_helper');
        $this->load->helper('image');
        $this->load->model('activities/Activities_model');
        $this->assign('index_channel','contents');

    }
    function index(){
        $page = $this->input->get('page');
        $page = $page > 0? $page: 1;
        $is_del = $this->input->get('is_del')>0?1:0;
        $is_sold = $this->input->get('is_sold')>0?1:0;
        $start = $this->input->get('started')>0?1:0;
        $condition = array(
            'is_del'=>$is_del,
            'is_sold'=>$is_sold
        );
        if($start>0){
            $condition['start_time>']=date('Y-m-d H:i:s',time()+3600*24);
        }

        $base_url = url_query($condition, '/m-admin/activity/index');

        $pagination = $this->Activities_model->pagination($base_url, $page, $condition, 'id desc');
        $this->session->set_userdata(
            'back_list_page',
            $this->get_self_url()
        );
        $this->assign('pagination', $pagination);
        $this->display();
    }
    function add(){
        $this->load->model('City_model');
        $condition = array('parent_code'=>'');
        $cities = $this->City_model->fetch_array($condition);
        $this->assign('province',$cities);
        $this->load->model('lecturer/Lecturer_model');
        $lecturer = $this->Lecturer_model->fetch_array(array('is_del'=>'0'));
        $this->load->model('place/Place_model');
        $place = $this->Place_model->fetch_array(array('is_del'=>'0'));
        $this->assign('lecturer',$lecturer);
        $this->assign('place',$place);

        $this->display();
    }
    function edit($id){
        $this->load->model('City_model');
        $condition = array('parent_code'=>'');
        $cities = $this->City_model->fetch_array($condition);
        if($id<1)
            $this->json_response(false,null,'id error');
        $activity = $this->Activities_model->getOne(array('id'=>$id));
        $this->assign('activity',$activity);
        $this->assign('province',$cities);
        $this->load->model('lecturer/Lecturer_model');
        $lecturer = $this->Lecturer_model->fetch_array(array('is_del'=>'0'));
        $this->load->model('place/Place_model');
        $place = $this->Place_model->fetch_array(array('is_del'=>'0'));
        $this->assign('lecturer',$lecturer);
        $this->assign('place',$place);
        $this->display();
    }
    /**
     * 发布活动 POST
     *
     * @return json
     */
    function  do_release(){
        if(!$this->input->post('title') or !$this->input->post('content'))
            $this->json_response(false,null,'50004');
        $data = array(
            'title'             =>  $this->input->post('title'),
            'cover'             =>  $this->input->post('cover'),
            'intro'             =>  $this->input->post('intro'),
            'theme'             =>  $this->input->post('theme'),
            'content'           =>  $this->input->post('content'),
            'created'           =>  date("Y-m-d H:i:s",time()),
            'price'             =>  floatval($this->input->post('price')),
            'start_time'        =>  $this->input->post('start_time'),
            'address'           =>  $this->input->post('address'),
//            'sale_price'        =>  $this->input->post('sale_price'),   //5人1000
            'special_info'      =>  $this->input->post('special_info'),
            'tags'              =>  $this->input->post('tags'),  //tag1,tag2,tag3,tag4
            'places'            =>  $this->input->post('places'),
            'lecturer_id'          =>  $this->input->post('lecturer_id'),
            'place_id'          =>  $this->input->post('place_id'),
            'tags'              =>  $this->input->post('tags')
        );
        if(!$this->Activities_model->insert($data)){
            $this->json_response(false,null,'发布活动失败');
        }
        $this->json_response(true,null,'发布活动成功');
    }


    /**
     * 修改活动 POST
     *
     * @return json
     */
    function do_change(){
        $data = array(
            'id'                =>  intval($this->input->post('id')),
            'title'             =>  $this->input->post('title'),
            'cover'             =>  $this->input->post('cover'),
            'intro'             =>  $this->input->post('intro'),
            'theme'             =>  $this->input->post('theme'),
            'content'           =>  $this->input->post('content'),
            'address'           =>  $this->input->post('address'),
            'price'             =>  floatval($this->input->post('price')),
            'start_time'        =>  $this->input->post('start_time'),
            'special_info'      =>  $this->input->post('special_info'),
            'tags'              =>  $this->input->post('tags'),  //tag1,tag2,tag3,tag4
            'places'            =>  $this->input->post('places'),
            'lecturer_id'          =>  $this->input->post('lecturer'),
            'place_id'          =>  $this->input->post('place_id'),
            'tags'              =>  $this->input->post('tags')
        );
        if(!$this->Activities_model->update($data)){
            $this->json_response(false,null,'更新活动失败');
        }
        $this->json_response(true,null,'更新活动成功');
    }


    /**
     * 下架活动
     *
     * @return json
     */
    function do_soldout(){
        $activity_id = intval($this->input->post('id'));
        if(!$activity_id)
            $this->json_response(false,null,'id error');
        $this->load->model('activities/Activities_model');
        $this->Activities_model->sold_out($activity_id);
        $this->json_response();
    }

    /**
     * 上架
     *
     * @return json
     */
    function do_sold(){
        $activity_id = intval($this->input->post('id'));
        if(!$activity_id)
            $this->json_response(false,null,'id error');

        $this->Activities_model->sold_in($activity_id);
        $this->json_response();
    }

    function del(){
        $activity_id = intval(trim($this->input->post('id')));
        if($activity_id<1){
            $this->json_response(false);
        }
        $article = $this->Activities_model->soft_delete($activity_id);
        if($article){
            $this->json_response(true);
        }
        $this->json_response(false);
    }
    function re_back(){
        $activity_id = intval(trim($this->input->post('id')));
        if($activity_id<1){
            $this->json_response(false);
        }
        $article = $this->Activities_model->reback($activity_id);
        if($article){
            $this->json_response(true);
        }
        $this->json_response(false);
    }
}